import BaseCommand from "@/core/Frame/BaseClass/BaseCommand"

declare global {
  interface Window {
    EarthDraw: any
  }
}

const PIE = window.PIE;
const EarthDraw = window.EarthDraw;

class AreaReductionCommand extends BaseCommand {
  viewer: any
  globe: any
  draw: any

  constructor(params: any) {
    super();
    this.viewer = params.viewer;
    this.globe = params.viewer.getGlobe();
  }

  execute(option: any) {
    this.draw = new EarthDraw.setup(this.viewer, {});
    this.draw.changeMode('draw_dragRect');
    this.draw.on('draw.commit', (e: any) => {
      this.draw.clearFeatures()
      const bounds = e.feature.vertexs;
      const bound = new PIE.Bounds(bounds[0][0], bounds[0][1], bounds[1][0], bounds[1][1]);
      let center = bound.getCenter()
      let zoom = this.globe.getZoom()
      zoom > 1 ? this.globe.setZoom(--zoom) : this.globe.setZoom(zoom);
      this.globe.setCenter(center);
      this.globe.refresh(false, false)
    })
  }

  release() {
    if (this.draw) {
      this.draw.changeMode('simple_select')
    }
  }

  isEnable(option: any) {
    return true
  }
}

export default AreaReductionCommand